package property.controller;

import com.alibaba.fastjson.JSONObject;
import property.pojo.Admin;
import property.pojo.Finance;
import property.pojo.Staff;
import property.service.FinanceService;
import property.service.impl.FinanceServiceImpl;
import property.utils.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = {"/Finance/*"})
public class FinanceServlet extends BaseServlet {

    private FinanceService financeService = new FinanceServiceImpl();

    public void addFinance(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();

        //数据发送简易身份验证
        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");
        if(character != 1){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }

        Finance finance = JSONObject.toJavaObject(jb.getJSONObject("insert"), Finance.class);
        finance.setfStaffId(((Staff)session.getAttribute("currentUser")).getsId());
        int i = financeService.financeInsert(finance);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "新增财务成功！");
        }else if (i==-2){
            jbReturn.put("code", 0);
            jbReturn.put("messages", "！");
        } else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }

    public void  deleteFinance(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();

        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");
        if(character == 0){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }

        int i = financeService.deleteFinance(jb.getInteger("fId"));
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "删除成功");
        } else if(i < 0){
            jbReturn.put("code", -1);
            jbReturn.put("messages", "关联数据未删除！");
        } else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }

    public void updateFinance(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();

        //数据发送简易身份性验证
        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");

        if(character == 0){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }

        Finance finance = JSONObject.toJavaObject(jb.getJSONObject("update"), Finance.class);
        int i = financeService.updateFinance(finance);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "修改财务成功");
        } else if(i < 0){
            jbReturn.put("code", -1);
            jbReturn.put("messages", "关联数据无法修改！");
        } else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }
}
